<template>
  <div class="panel-card">
    <div class="panel-header">
      <h3 class="panel-title">文档列表</h3>
      <div class="panel-actions">
        <select class="filter-select" v-model="localFilterType" @change="handleFilterChange">
          <option value="all">全部类型</option>
          <option value="pdf">PDF</option>
          <option value="doc">Word</option>
          <option value="docx">Word</option>
          <option value="xls">Excel</option>
          <option value="xlsx">Excel</option>
          <option value="csv">CSV</option>
          <option value="txt">文本</option>
          <option value="md">Markdown</option>
        </select>
      </div>
    </div>
    <div class="documents-list">
      <!-- 加载状态 -->
      <div v-if="loading" class="loading-container">
        <div class="loading-spinner"></div>
        <span>加载中...</span>
      </div>
      
      <!-- 空状态 -->
      <div v-else-if="documents.length === 0" class="empty-state">
        <div class="empty-icon">📚</div>
        <p>暂无文档</p>
        <span>点击"上传文档"按钮添加新文档</span>
      </div>
      
      <!-- 文档列表 -->
      <div v-else class="document-items-container">
        <div 
          v-for="document in documents" 
          :key="document.filename"
          class="document-item"
        >
          <div class="document-info">
            <div class="document-icon">{{ document.icon }}</div>
            <div class="document-details">
              <h4>{{ document.filename }}</h4>
              <div class="document-meta">
                <span class="meta-item">{{ document.sizeFormatted }}</span>
                <span class="meta-item">{{ document.modifiedTimeFormatted }}</span>
                <span 
                  class="meta-item"
                  :class="{
                    'status-success': document.status === 'indexed',
                    'status-processing': document.status === 'processing',
                    'status-error': document.status === 'error'
                  }"
                >
                  {{ getStatusText(document.status) }}
                </span>
              </div>
            </div>
          </div>
          <div class="document-actions">
            <button 
              class="action-btn"
              @click="handleViewDocument(document)"
              title="查看"
            >
              👁️
            </button>
            <button 
              class="action-btn"
              @click="handleEditDocument(document)"
              title="编辑"
            >
              ✏️
            </button>
            <button 
              class="action-btn"
              @click="handleDeleteDocument(document)"
              title="删除"
            >
              🗑️
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DocumentList',
  props: {
    documents: {
      type: Array,
      default: () => []
    },
    loading: {
      type: Boolean,
      default: false
    },
    filterType: {
      type: String,
      default: 'all'
    }
  },
  emits: ['filterChange', 'viewDocument', 'editDocument', 'deleteDocument'],
  data() {
    return {
      localFilterType: this.filterType
    };
  },
  watch: {
    filterType(newVal) {
      this.localFilterType = newVal;
    }
  },
  methods: {
    handleFilterChange() {
      this.$emit('filterChange', this.localFilterType);
    },
    handleViewDocument(document) {
      this.$emit('viewDocument', document);
    },
    handleEditDocument(document) {
      this.$emit('editDocument', document);
    },
    handleDeleteDocument(document) {
      this.$emit('deleteDocument', document);
    },
    getStatusText(status) {
      const statusMap = {
        'indexed': '已索引',
        'processing': '索引中',
        'error': '索引失败',
        'not_indexed': '未索引'
      };
      return statusMap[status] || status;
    }
  }
};
</script>

<style scoped>
.panel-card {
  background: white;
  border-radius: 10px;
  padding: 25px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.panel-title {
  font-size: 1.3rem;
  color: #2c3e50;
  margin: 0;
}

.panel-actions {
  display: flex;
  gap: 10px;
}

.filter-select {
  padding: 8px 12px;
  border: 1px solid #e1e5e9;
  border-radius: 6px;
  background: white;
  font-size: 0.9rem;
}

.documents-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.document-items-container {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.document-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  border-radius: 8px;
  border: 1px solid #e1e5e9;
  transition: all 0.3s ease;
}

.document-item:hover {
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
  border-color: #dee2e6;
}

.document-info {
  display: flex;
  align-items: center;
  gap: 15px;
  flex: 1;
}

.document-icon {
  font-size: 2rem;
}

.document-details h4 {
  margin: 0 0 8px 0;
  color: #2c3e50;
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.document-meta {
  display: flex;
  gap: 15px;
}

.meta-item {
  font-size: 0.85rem;
  color: #6c757d;
}

.status-success {
  color: #27AE60;
  font-weight: 500;
}

.status-processing {
  color: #F1C40F;
  font-weight: 500;
}

.status-error {
  color: #E74C3C;
  font-weight: 500;
}

.document-actions {
  display: flex;
  gap: 10px;
}

.action-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid #e1e5e9;
  background: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.action-btn:hover {
  background: #f8f9fa;
  transform: translateY(-2px);
}

.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  color: #6c757d;
}

.loading-spinner {
  border: 4px solid #f3f3f3;
  border-top: 4px solid #1976d2;
  border-radius: 50%;
  width: 30px;
  height: 30px;
  animation: spin 1s linear infinite;
  margin-bottom: 10px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  text-align: center;
  color: #6c757d;
}

.empty-icon {
  font-size: 3rem;
  margin-bottom: 15px;
}

.empty-state p {
  font-size: 1.2rem;
  margin: 0 0 10px 0;
  color: #495057;
}

.empty-state span {
  font-size: 0.9rem;
}

@media (max-width: 768px) {
  .document-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }
  
  .document-actions {
    align-self: flex-end;
  }
  
  .document-meta {
    flex-wrap: wrap;
  }
  
  .document-details h4 {
    max-width: 100%;
  }
}
</style>